let color = "{COOKIE_color}";

$(function () {
    $.post("./?/settings",{
        action: "getSettings",
        content: ""
    },function (text) {
        let data = JSON.parse(text);
        /*console.log(data);*/

        let $enableLogIP = $("#enableLogIP");
        let $enableLogIPEncrypt = $("#enableLogIPEncrypt");
        let $cleanLogIP = $("#cleanLogIP");
        let $adminPassword = $("#AdminPassword");
        let $setAdminPassword =$("#setAdminPassword");
        $enableLogIP[0].checked = data["enableLogIP"]==="yes";
        $enableLogIPEncrypt[0].checked = data["enableLogIPEncrypt"]==="yes";
        if (!$enableLogIP[0].checked){
            $enableLogIPEncrypt.attr("disabled","disabled").next().addClass("disabled").parent().parent().addClass("disabled");
            $cleanLogIP.addClass("disabled").attr("disabled","disabled");
        }

        $adminPassword.val($.cookie("admin_code"));

        $enableLogIP.click(function () {
            let x = this.checked ? "yes" : "no";
            $.post("./?/settings",{
                action: "setEnableLogIP",
                content: x
            },function (text) {
                console.log(text);
                location.reload();
            });
        });
        $enableLogIPEncrypt.click(function () {
            let x = this.checked ? "yes" : "no";
            $.post("./?/settings",{
                action: "setEnableLogIPEncrypt",
                content: x
            },function (text) {
                console.log(text);
                location.reload();
            });
        });
        $cleanLogIP.click(function () {
            if (!$(this).hasClass("disabled")) {
                if (confirm("是否清空访问记录？")) {
                    $.post("./?/settings", {
                        action: "cleanLogIP",
                        content: ""
                    }, function (text) {
                        console.log(text);
                        location.reload();
                    });
                }
            }
        });
        $setAdminPassword.click(function () {
            let x = $adminPassword.val();
            if (x){
                $.post("./?/settings",{
                    action: "setAdminPassword",
                    content: x
                },function (text){
                    console.log(text);
                    location.reload();
                });
            }else {
                alert("管理员密码不能为空");
            }
        });
    });
});